package exsun.jt808.data.parseUtil;


//import com.exsun.commonData.upload_data.*;
import exsun.jt808.data.distribute_data.Qingdao.AlarmSignIdInfo;
import exsun.jt808.data.upload_data.GpsAlarmType;
import exsun.jt808.data.upload_data.GpsAlarms;
import exsun.jt808.data.upload_data.GpsData;
import exsun.jt808.data.upload_data.GpsPropertyType;
import exsun.jt808.data.upload_data.jt808.AlarmDetail;
import exsun.jt808.data.util.parse.DecoderUtils;
import exsun.jt808.data.util.parse.HexStrUtil;
import exsun.jt808.data.util.parse.TimeUtil;

import java.util.ArrayList;
import java.util.List;

public class Jsalt12Alarm {

    public static int getAlarmType(int type,int alarmType){
        int result = GpsAlarmType.NONE;
        switch (type){
            case 0x64:
                result = GetDriverAssistanceAlarmType(alarmType);
                break;
            case 0x65:
                result = GetDriverStatusAlarmType(alarmType);
                break;
            case 0x66:
                result = GpsAlarmType.TyrePressure;
                break;
            case 0x67:
                result = GetBlindSpotMonitorAlarmType(alarmType);
                break;
            default:
                    break;
        }
        return result;

    }


    //苏标报警
    public static void AdvancedDriverAssistanceAlarm(GpsData gps, byte[] content){
        int alarmType = GetDriverAssistanceAlarmType(DecoderUtils.parseIntFromBytes(content, 5, 1));
        gps.alm.add(alarmType);
        //gps.setAfg(HexStrUtil.encodeHex(content,31,16));
        AlarmDetail detail = new AlarmDetail();
        detail.lat = Integer.parseInt(HexStrUtil.encodeHex(content, 15, 4), 16) / 1000000.0;
        detail.lng = Integer.parseInt(HexStrUtil.encodeHex(content, 19, 4), 16) / 1000000.0;
        detail.afg = HexStrUtil.encodeHex(content,31,16);
        detail.abe = Integer.parseInt(HexStrUtil.encodeHex(content,4,1));
        long timestamp = TimeUtil.parseTime(HexStrUtil.encodeHex(content, 23, 6), TimeUtil.dateFormatyyMMddHHmmss);
        detail.setAdt(TimeUtil.getDateByFormat(timestamp, TimeUtil.dateFormatYMDHMS));
        if (TimeUtil.getOffectDay(TimeUtil.getNowTimeStamp(), timestamp) > 7)
        {
            long nowstamp = TimeUtil.getNowTimeStamp();
            detail.setAdt(TimeUtil.getDateByFormat(nowstamp, TimeUtil.dateFormatYMDHMS));
        }
        gps.adl.put(alarmType,detail);
    }

    public static void DriverStatusMonitoringAlarm(GpsData gps, byte[] content){
        int alarmType = GetDriverStatusAlarmType(DecoderUtils.parseIntFromBytes(content, 5, 1));
        gps.alm.add(alarmType);
        //gps.setAfg(HexStrUtil.encodeHex(content,31,16));
        AlarmDetail detail = new AlarmDetail();
        detail.lat = Integer.parseInt(HexStrUtil.encodeHex(content, 15, 4), 16) / 1000000.0;
        detail.lng = Integer.parseInt(HexStrUtil.encodeHex(content, 19, 4), 16) / 1000000.0;
        detail.afg = HexStrUtil.encodeHex(content,31,16);
        detail.abe = Integer.parseInt(HexStrUtil.encodeHex(content,4,1));
        long timestamp = TimeUtil.parseTime(HexStrUtil.encodeHex(content, 23, 6), TimeUtil.dateFormatyyMMddHHmmss);
        detail.setAdt(TimeUtil.getDateByFormat(timestamp, TimeUtil.dateFormatYMDHMS));
        if (TimeUtil.getOffectDay(TimeUtil.getNowTimeStamp(), timestamp) > 7)
        {
            long nowstamp = TimeUtil.getNowTimeStamp();
            detail.setAdt(TimeUtil.getDateByFormat(nowstamp, TimeUtil.dateFormatYMDHMS));
        }
        gps.adl.put(alarmType,detail);
    }

    public static void TirePressureMonitoringAlarm(GpsData gps, byte[] content){
        //gps.setAfg(HexStrUtil.encodeHex(content,24,16));
        int count = DecoderUtils.parseIntFromBytes(content, 39, 1);
        int index = 40;
        for(int n=0;n<count;n++)
        {
            index +=1;
            List<Integer> alarmTypes = GetTirePressureAlarmTypes(DecoderUtils.parseIntFromBytes(content, index, 2));
            gps.alm.addAll(alarmTypes);
            index +=8;
        }
        AlarmDetail detail = new AlarmDetail();
        detail.lat = Integer.parseInt(HexStrUtil.encodeHex(content, 8, 4), 16) / 1000000.0;
        detail.lng = Integer.parseInt(HexStrUtil.encodeHex(content, 12, 4), 16) / 1000000.0;
        detail.afg = HexStrUtil.encodeHex(content,24,16);
        detail.abe = Integer.parseInt(HexStrUtil.encodeHex(content,4,1));
        long timestamp = TimeUtil.parseTime(HexStrUtil.encodeHex(content, 16, 6), TimeUtil.dateFormatyyMMddHHmmss);
        detail.setAdt(TimeUtil.getDateByFormat(timestamp, TimeUtil.dateFormatYMDHMS));
        if (TimeUtil.getOffectDay(TimeUtil.getNowTimeStamp(), timestamp) > 7)
        {
            long nowstamp = TimeUtil.getNowTimeStamp();
            detail.setAdt(TimeUtil.getDateByFormat(nowstamp, TimeUtil.dateFormatYMDHMS));
        }
        gps.adl.put(GpsAlarmType.TireAlarm,detail);

    }

    public static void BlindSpotMonitoringAlarm(GpsData gps, byte[] content){
        int alarmType = GetBlindSpotMonitorAlarmType(DecoderUtils.parseIntFromBytes(content, 5, 1));
        gps.alm.add(alarmType);
        //gps.setAfg(HexStrUtil.encodeHex(content,24,16));
        AlarmDetail detail = new AlarmDetail();
        detail.lat = Integer.parseInt(HexStrUtil.encodeHex(content, 9, 4), 16) / 1000000.0;
        detail.lng = Integer.parseInt(HexStrUtil.encodeHex(content, 13, 4), 16) / 1000000.0;
        detail.afg = HexStrUtil.encodeHex(content,25,16);
        detail.abe = Integer.parseInt(HexStrUtil.encodeHex(content,4,1));
        long timestamp = TimeUtil.parseTime(HexStrUtil.encodeHex(content, 17, 6), TimeUtil.dateFormatyyMMddHHmmss);
        detail.setAdt(TimeUtil.getDateByFormat(timestamp, TimeUtil.dateFormatYMDHMS));
        if (TimeUtil.getOffectDay(TimeUtil.getNowTimeStamp(), timestamp) > 7)
        {
            long nowstamp = TimeUtil.getNowTimeStamp();
            detail.setAdt(TimeUtil.getDateByFormat(nowstamp, TimeUtil.dateFormatYMDHMS));
        }
        gps.adl.put(alarmType,detail);
    }


    /**
     * 货厢识别信息上报
     */
    public static void BoxDistinguish(GpsData gps ,byte[] content){
        int alarmType = BoxDistinguishAlarmType(DecoderUtils.parseIntFromBytes(content,5,1));
        gps.alm.add(alarmType);
        AlarmDetail detail = new AlarmDetail();
        detail.lat = Integer.parseInt(HexStrUtil.encodeHex(content, 10, 4), 16) / 1000000.0;
        detail.lng = Integer.parseInt(HexStrUtil.encodeHex(content, 14, 4), 16) / 1000000.0;
        detail.afg = HexStrUtil.encodeHex(content,26,16);
        detail.abe = Integer.parseInt(HexStrUtil.encodeHex(content,4,1));
        long timestamp = TimeUtil.parseTime(HexStrUtil.encodeHex(content, 18, 6), TimeUtil.dateFormatyyMMddHHmmss);
        detail.setAdt(TimeUtil.getDateByFormat(timestamp, TimeUtil.dateFormatYMDHMS));
        if (TimeUtil.getOffectDay(TimeUtil.getNowTimeStamp(), timestamp) > 7)
        {
            long nowStamp = TimeUtil.getNowTimeStamp();
            detail.setAdt(TimeUtil.getDateByFormat(nowStamp, TimeUtil.dateFormatYMDHMS));
        }
        gps.adl.put(alarmType,detail);
    }


    /**
     * 盲区
     */
    public static void BlindArea(GpsData gps ,byte[] content){
        int alarmType = BlindAreaAlarmType(DecoderUtils.parseIntFromBytes(content,5,1));
        gps.alm.add(alarmType);
        AlarmDetail detail = new AlarmDetail();
        detail.lat = Integer.parseInt(HexStrUtil.encodeHex(content, 9, 4), 16) / 1000000.0;
        detail.lng = Integer.parseInt(HexStrUtil.encodeHex(content, 13, 4), 16) / 1000000.0;
        detail.afg = HexStrUtil.encodeHex(content,25,16);
        detail.abe = Integer.parseInt(HexStrUtil.encodeHex(content,4,1));
        long timestamp = TimeUtil.parseTime(HexStrUtil.encodeHex(content, 17, 6), TimeUtil.dateFormatyyMMddHHmmss);
        detail.setAdt(TimeUtil.getDateByFormat(timestamp, TimeUtil.dateFormatYMDHMS));
        if (TimeUtil.getOffectDay(TimeUtil.getNowTimeStamp(), timestamp) > 7)
        {
            long nowStamp = TimeUtil.getNowTimeStamp();
            detail.setAdt(TimeUtil.getDateByFormat(nowStamp, TimeUtil.dateFormatYMDHMS));
        }
        gps.adl.put(alarmType,detail);
    }


    public static int GetDriverAssistanceAlarmType(int alarmType)
    {
        int type = GpsAlarmType.NONE;
        switch (alarmType)
        {
            case 0x1:
                type = GpsAlarmType.FrontCrash;
                break;
            case 0x2:type = GpsAlarmType.LaneDeparture;
                break;
            case 0x3:
                type = GpsAlarmType.VehiclesTooClose;
                break;
            case 0x4:
                type = GpsAlarmType.PedestrianCrash;
                break;
            case 0x5:
                type = GpsAlarmType.FrequentlyChangeLanes;
                break;
            case 0x6:
                type = GpsAlarmType.OverLimitOfRoadSigns;
                break;
            case 0x7:
                type = GpsAlarmType.Barrier;
                break;
            case 0x10:
                type = GpsAlarmType.RoadSignIdentification;
                break;
            case 0x11:
                type = GpsAlarmType.TakeInitiativeToCapture;
                break;
            default:
                break;
        }
        return  type;
    }

    public static int GetDriverStatusAlarmType(int alarmType)
    {
        int type = GpsAlarmType.NONE;
        switch (alarmType)
        {
            case 0x1:
                type = GpsAlarmType.AdasFatigueDriving;
                break;
            case 0x2:
                type = GpsAlarmType.MakingCalls;
                break;
            case 0x3:
                type = GpsAlarmType.Smoking;
                break;
            case 0x4:
                type = GpsAlarmType.DistractedDriving;
                break;
            case 0x5:
                type = GpsAlarmType.DriverAnomaly;
                break;
            case 0x10:
                type = GpsAlarmType.AutomaticCapture;
                break;
            case 0x11:
                type = GpsAlarmType.DriverChange;
                break;
            default:
                break;
        }
        return  type;
    }

    public static List<Integer> GetTirePressureAlarmTypes(int number)
    {
        List<Integer> types = new ArrayList<Integer>();
        if(((number & 0x02) >> 1) == 1)
        {
            types.add(GpsAlarmType.TyrePressureHigh);
        }
        if(((number & 0x04) >> 1) == 1)
        {
            types.add(GpsAlarmType.TyrePressureLow);
        }
        if(((number & 0x08) >> 3) == 1)
        {
            types.add(GpsAlarmType.TyreTemperatureHigh);
        }
        if(((number & 0x10) >> 4) == 1)
        {
            types.add(GpsAlarmType.SensorAnomaly);
        }
        if(((number & 0x20) >> 5) == 1)
        {
            types.add(GpsAlarmType.TyrePressureUnbalance);
        }
        if(((number & 0x40) >> 6) == 1)
        {
            types.add(GpsAlarmType.SlowLeak);
        }
        if(((number & 0x80) >> 7) == 1)
        {
            types.add(GpsAlarmType.LowBattery);
        }
        return types;
    }

    public static int GetBlindSpotMonitorAlarmType(int alarmType)
    {
        int type = GpsAlarmType.NONE;
        switch (alarmType)
        {
            case 0x1:
                type = GpsAlarmType.CloseBehind;
                break;
            case 0x2:
                type = GpsAlarmType.LeftSideOfRear;
                break;
            case 0x3:
                type = GpsAlarmType.RightSideOfRear;
                break;
            default:
                break;
        }
        return  type;
    }

    public static int BoxDistinguishAlarmType(int alarmType){
        int type = GpsAlarmType.NONE;
        switch (alarmType)
        {
            case 0x01:
                type = GpsAlarmType.CameraCover;
                break;
            case 0x02:
                type = GpsAlarmType.GoodsDetectedNotSealed;
                break;
            case 0x03:
                type = GpsAlarmType.GoodsIllegalUnloading;
                break;
            case 0x04:
                type = GpsAlarmType.CarriageCameraDeviate;
            default:
                break;
        }
        return  type;
    }

    public static int BlindAreaAlarmType(int alarmType){
        int type = GpsAlarmType.NONE;
        switch (alarmType)
        {
            case 0x01:
                type = GpsAlarmType.BlindAreaAhead;
                break;
            case 0x11:
                type = GpsAlarmType.BlindAreaRightOne;
                break;
            case 0x12:
                type = GpsAlarmType.BlindAreaRightTwo;
                break;
            case 0x13:
                type = GpsAlarmType.BlindAreaRightThree;
            default:
                break;
        }
        return  type;
    }
}
